package yxy.apple.util;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;

import java.util.*;

/**
 * Author HYB
 * Date 2021/12/7
 * Time 20:23
 */
public class BeanUtils {
    /**
     * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
     */

    private static Mapper mapper = DozerBeanMapperBuilder.buildDefault();


    public static <T> T map(Object source, Class<T> destinationClass) {
        return mapper.map(source, destinationClass);
    }

    /**
     * 基于Dozer将对象A的值拷贝到对象B中.
     */
    public static void copy(Object source, Object destinationObject) {
        mapper.map(source, destinationObject);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<>();
        for (Object sourceObject : sourceList) {
            T destinationObject = mapper.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }
}